﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Phone.Controls;
using System.Diagnostics;

namespace Organizer.ViewModel
{
    public enum ViewType { Home, Session, Statistics, Organizations, TaskList, TaskEdit, CategoryList, NoteList, NoteEdit, Settings, Mans, About, ToDo, Hub }

    public class ApplicationController
    {
        static ApplicationController m_instance;
        Dictionary<ViewType, Uri> m_Views;

        ApplicationController()
        {
            m_Views = new Dictionary<ViewType, Uri>();

            //register views with controller
            Register(ViewType.Home, new Uri("/", UriKind.Relative));
            Register(ViewType.TaskList, new Uri("/Tasks", UriKind.Relative));
            Register(ViewType.TaskEdit, new Uri("/TaskEdit", UriKind.Relative));
            Register(ViewType.Organizations, new Uri("/Organizations", UriKind.Relative));
            Register(ViewType.NoteList, new Uri("/Notes", UriKind.Relative));
            Register(ViewType.NoteEdit, new Uri("/NoteEdit", UriKind.Relative));
            Register(ViewType.CategoryList, new Uri("/CategoryList", UriKind.Relative));
            Register(ViewType.Settings, new Uri("/Settings", UriKind.Relative));
            Register(ViewType.Mans, new Uri("/Mans", UriKind.Relative));
            Register(ViewType.ToDo, new Uri("/Main", UriKind.Relative));
            Register(ViewType.Hub, new Uri("/Hub", UriKind.Relative));
        }

        public static ApplicationController Default
        {
            get
            {
                if (m_instance == null)
                    m_instance = new ApplicationController();

                return m_instance;
            }
        }

        void Register(ViewType type, Uri address)
        {
            if (m_Views.ContainsKey(type)) //update
            {
                m_Views[type] = address;
                return;
            }

            m_Views.Add(type, address); //add
        }

        void UnRegister(ViewType type)
        {
            if (m_Views.ContainsKey(type))
                m_Views.Remove(type);
        }

        public void NavigateTo(ViewType type)
        {
            if (!m_Views.ContainsKey(type))
                return;

            Uri address = m_Views[type];

            //magic code here!
            PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame;
            Debug.Assert(root != null, "Root is null");
            root.Navigate(address);
        }
    }
}
